package com.j.lemon.learn.basic.io;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.j.lemon.learn.basic.object.justtest.Employee;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @Author lijunjun
 * @Date 2019-03-11 15:59
 * @Description
 */
public class Test1 {

    @Test
    void test() throws IOException {
        //字节输出流
        FileOutputStream fos = new FileOutputStream("/Users/lijunjun/demo.txt",true);
        //字符输出流
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        //缓冲字符输出流
        PrintWriter printWriter = new PrintWriter(osw,true);
//        printWriter.print("a");
//        printWriter.print("b");
//        printWriter.flush();
//        printWriter.println("这是第一行");
//        printWriter.close();
        //字节输入流
        FileInputStream fis = new FileInputStream("/Users/lijunjun/demo.txt");
        //字符输入流
        InputStreamReader isr = new InputStreamReader(fis);
        //缓冲字符输入流
        BufferedReader bufferedReader = new BufferedReader(isr);

        String content=bufferedReader.readLine();
        System.out.println(content);

        //缓冲字节输入流
        BufferedInputStream bufferedInputStream= new BufferedInputStream(fis);
        //缓冲字节输出流
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fos);
    }


    @Test
    void data() throws IOException {
        //二进制输出流
        DataOutputStream dataOutputStream = new DataOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream("/Users/lijunjun/work/file.log")));
        for (int i = 0; i <100; i++) {
            dataOutputStream.writeInt(i);

        }
        dataOutputStream.flush();
        dataOutputStream.close();

        //二进制输出流
        DataInputStream dataInputStream = new DataInputStream(
                new BufferedInputStream(
                        new FileInputStream("/Users/lijunjun/work/file.log")));
        int i = dataInputStream.readInt();
        System.out.println(i);
        dataInputStream.close();
    }

    @Test
    void random() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("/Users/lijunjun/work/file.log","rw");
        randomAccessFile.seek(1L);
        randomAccessFile.write(4);
    }

    @Test
    void zip() throws IOException{
        ZipInputStream zipInputStream = new ZipInputStream(
                        new FileInputStream("/Users/lijunjun/Downloads/sitesucker.zip"));
        ZipEntry zipEntry;
        int available = zipInputStream.available();
        System.out.println(available);
        for (int i = 0; i < available; i++) {
            zipEntry=zipInputStream.getNextEntry();
            System.out.println(zipEntry.getName()+":"+zipEntry.getSize()+":"+zipEntry.getComment());

        }

        ZipOutputStream zipOutputStream = new ZipOutputStream(
                new BufferedOutputStream(
                        new FileOutputStream("/Users/lijunjun/Downloads/sitesucker.zip")
                )
        );

        ZipEntry zipEntry1 = new ZipEntry("1.txt");
        zipEntry1.setComment("hhh1");
        zipOutputStream.putNextEntry(zipEntry1);
        zipEntry1.setComment("222222");

        zipOutputStream.write(12);

        zipOutputStream.closeEntry();
        zipOutputStream.close();
    }

    @Test
    void objectIO() throws IOException, ClassNotFoundException {
        //对象序列化
//        Employee employee = new Employee(18,"ljj");
//        Employee employee1 = new Employee(16,"sxq");
//        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/lijunjun/work/file.log"));
//        objectOutputStream.writeObject(employee);
//        objectOutputStream.writeObject(employee1);
//        objectOutputStream.flush();
//        objectOutputStream.close();
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("/Users/lijunjun/work/file.log"));
        Object o = objectInputStream.readObject();
        if(o instanceof Employee){
            Employee e = (Employee)o;
            System.out.println(e.getName());
            System.out.println(e.getAge());
        }else{
            System.out.println("error");
        }
    }

    @Test
    void json(){
        String s = "{\n" +
                "  \"fullPath\": \"api/data/personCriminal\",\n" +
                "  \"method\": \"personCriminal\",\n" +
                "  \"partnerCode\": \"shunfeng\",\n" +
                "  \"body\": \"{\\\"partnerCode\\\":\\\"shunfeng\\\",\\\"partnerKey\\\":\\\"fe1639c9ddd946ce9ae4fdc73be9a156\\\",\\\"name\\\":\\\"张尧贵\\\",\\\"idNumber\\\":\\\"330624198004250936\\\"}\",\n" +
                "  \"path\": \"data\",\n" +
                "  \"partnerKey\": \"fe1639c9ddd946ce9ae4fdc73be9a156\",\n" +
                "  \"root\": \"api\",\n" +
                "  \"callIp\": \"218.17.224.132\",\n" +
                "  \"seqId\": \"1552358925830A919021D643447CD330\"\n" +
                "}";
        JSONObject jsonObject = JSONObject.parseObject(s);
        String body = jsonObject.getString("body");
        JSONObject jsonBody = JSONObject.parseObject(body);
        jsonObject.put("body",jsonBody);

        String s1 = JSON.toJSONString(jsonObject);
        System.out.println(s1);


        System.out.println(20+55+29+50+33+15+48+33+10+17+17);
    }
}
